package com.ruoyi.user.controller;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.JwtUtils;
import com.ruoyi.user.domain.bo.MyCourseBo;
import com.ruoyi.user.domain.bo.OneClassify;
import com.ruoyi.user.domain.bo.RegisterVo;
import lombok.RequiredArgsConstructor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.user.domain.vo.UcenterMemberVo;
import com.ruoyi.user.domain.bo.UcenterMemberBo;
import com.ruoyi.user.service.IUcenterMemberService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户
 *
 * @author 孟磊
 * @date 2022-08-18
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/member")
public class UcenterMemberController extends BaseController {

    private final IUcenterMemberService iUcenterMemberService;

    /**
     * 查询用户列表
     */
    @SaCheckPermission("system:member:list")
    @GetMapping("/list")
    public TableDataInfo<UcenterMemberVo> list(UcenterMemberBo bo, PageQuery pageQuery) {
        return iUcenterMemberService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出用户列表
     */
    @SaCheckPermission("system:member:export")
    @Log(title = "用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(UcenterMemberBo bo, HttpServletResponse response) {
        List<UcenterMemberVo> list = iUcenterMemberService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户", UcenterMemberVo.class, response);
    }

    //查询所有用户
    @Anonymous
    @GetMapping("query")
    public R query(){
        return iUcenterMemberService.query();
    }

    //查询一级分类的交易额
    @Anonymous
    @GetMapping("queryclassify")
    public R queryclassify(){
        List<OneClassify> queryclassify = iUcenterMemberService.queryclassify();
        return R.ok(queryclassify);
    }
    //老师热度前六名
    @Anonymous
    @GetMapping("queryTeacher")
    public R queryTeacher(){
        List<OneClassify> queryTeacher = iUcenterMemberService.queryTeacher();
        return R.ok(queryTeacher);
    }
    //试卷的平均分
    @Anonymous
    @GetMapping("average")
    public R average(){
        List<OneClassify> average = iUcenterMemberService.average();
        return R.ok(average);
    }
    //上一周交易流水
    @Anonymous
    @GetMapping("week")
    public R week(){
        List<OneClassify> week = iUcenterMemberService.week();
        return R.ok(week);
    }
    /**
     * 获取用户详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:member:query")
    @GetMapping("/{id}")
    public R<UcenterMemberVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(iUcenterMemberService.queryById(id));
    }


    /**
     * 新增用户
     */
    @SaCheckPermission("system:member:add")
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody UcenterMemberBo bo) {
//        return toAjax(iUcenterMemberService.insertByBo(bo) ? 1 : 0);
        return iUcenterMemberService.insertByBo(bo);
    }

    //手机号密码登录
    @PostMapping("login")
    @Anonymous
    public R login(@RequestBody UcenterMemberBo bo){
        return iUcenterMemberService.login(bo);
    }


    //根据token拿到数据
    @GetMapping("getInfo")
    @Anonymous
    public R getInfo(HttpServletRequest request){
        String memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMemberVo ucenterMemberVo = iUcenterMemberService.queryById(memberIdByJwtToken);
        return R.ok(ucenterMemberVo);

    }

    //注册 register
    @Anonymous
    @PostMapping("/register")
    public R register(@RequestBody RegisterVo ro){
        return iUcenterMemberService.register(ro);
    }
    /**
     * 修改用户
     */
//    @SaCheckPermission("system:member:edit")
    @Anonymous
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
//"updateuser"
    public R edit(@Validated(EditGroup.class) @RequestBody UcenterMemberBo bo) {
//        return toAjax(iUcenterMemberService.updateByBo(bo) ? 1 : 0);
        return iUcenterMemberService.updateByBo(bo);
    }

    //

    /**
     * 删除用户
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:member:remove")
    @Log(title = "用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iUcenterMemberService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
//        return iUcenterMemberService.deleteWithValidByIds(Arrays.asList(ids);
    }

    //我的课程
    @Anonymous
    @GetMapping("course/{id}")
    public R mycourse(@PathVariable int id){
        List<MyCourseBo> list = iUcenterMemberService.mycourse(id);
        return R.ok(list);
    }
}
